package com.xuda.eduservice.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuda.common.utils.R;
import com.xuda.eduservice.entity.EduTeacher;
import com.xuda.eduservice.entity.vo.TeacherQuery;
import com.xuda.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author xudada
 * @since 2022-07-26
 */
@Api(value = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin 跨域可以使用网关进行解决,在网关中已经使用全局配置了
public class EduTeacherController {

    @Resource
    private EduTeacherService eduTeacherService;

    @GetMapping("/list")
    public R findAllTeacher() {
       List<EduTeacher> list = eduTeacherService.list(null);

       return R.ok().data("data",list);
    }

    /**
     * 逻辑删除讲师
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R removeTeacher(@PathVariable("id") String id) {
        boolean b = eduTeacherService.removeById(id);
        if (b) {
            return R.ok();
        }
        return R.error();
    }

    //分页查询
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageTeacher(@PathVariable long current,
                         @PathVariable long limit) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        eduTeacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords(); //数据集合
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data("data",map);
    }

    //模糊查询返回分页数据

    /**
     * 多条件查询带分页
     * @param current 当前页
     * @param limit  分页条数
     * @param teacherQuery Vo查询封装类
     * @return
     * @RequestBody(required = false) 需要POST提交，且required可以不需要
     */
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //构造条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();//讲师name
        Integer level = teacherQuery.getLevel();//讲师等级
        String begin = teacherQuery.getBegin();//开始时间
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.like("level",level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create",begin); //大于
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create",end); //小于
        }
        queryWrapper.orderByDesc("gmt_create"); //通过时间进行降序
        //调用方法实现条件查询分页
        eduTeacherService.page(pageTeacher,queryWrapper);
        long total = pageTeacher.getTotal(); //记录总数
        List<EduTeacher> records = pageTeacher.getRecords(); //数据集合
       // HashMap<String, Object> map = new HashMap<>();
       // map.put("total",total);
       // map.put("records",records);
        return R.ok().data("total",total).data("rows",records);
    }

    //新增加讲师
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        return eduTeacherService.save(eduTeacher)? R.ok():R.error();
    }

    //查询讲师
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher byId = eduTeacherService.getById(id);
        return R.ok().data("teacher",byId);
    }


    //更新讲师
    @ApiOperation(value = "更新讲师")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@ApiParam(name = "eduTeacher", value = "讲师对象", required = true)
                           @RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

